#!/bin/bash

# Redis-rs2 功能修复验证脚本
# 测试哈希操作、DECRBY命令和CLUSTER命令是否正常工作

echo "🧪 Redis-rs2 功能修复验证测试"
echo "==============================="
echo ""

# 函数：测试Redis命令
test_redis_command() {
    local server_port=$1
    local description=$2
    local commands=("${@:3}")
    
    echo "📋 测试: $description (端口 $server_port)"
    echo "-----------------------------------"
    
    for cmd in "${commands[@]}"; do
        echo "执行: $cmd"
        result=$(echo "$cmd" | nc localhost $server_port 2>/dev/null | head -1)
        echo "结果: $result"
        echo ""
    done
}

# 测试哈希操作修复
echo "🔧 测试 1: 哈希操作修复 (HSET/HGET)"
hash_commands=(
    "*3\r\n\$4\r\nHSET\r\n\$8\r\nmyhash\r\n\$5\r\nfield1\r\n\$6\r\nvalue1\r\n"
    "*3\r\n\$4\r\nHGET\r\n\$8\r\nmyhash\r\n\$5\r\nfield1\r\n"
    "*2\r\n\$4\r\nTYPE\r\n\$6\r\nmyhash\r\n"
    "*3\r\n\$4\r\nHSET\r\n\$8\r\nmyhash\r\n\$5\r\nfield2\r\n\$6\r\nvalue2\r\n"
    "*3\r\n\$4\r\nHGET\r\n\$8\r\nmyhash\r\n\$5\r\nfield2\r\n"
)

# 测试DECRBY命令修复  
echo "🔧 测试 2: DECRBY命令修复"
decrby_commands=(
    "*3\r\n\$3\r\nSET\r\n\$7\r\ncounter\r\n\$2\r\n10\r\n"
    "*2\r\n\$4\r\nDECR\r\n\$7\r\ncounter\r\n"
    "*3\r\n\$6\r\nDECRBY\r\n\$7\r\ncounter\r\n\$1\r\n3\r\n"
    "*2\r\n\$3\r\nGET\r\n\$7\r\ncounter\r\n"
)

# 测试CLUSTER命令（需要集群服务器）
echo "🔧 测试 3: CLUSTER命令修复"
cluster_commands=(
    "*2\r\n\$7\r\nCLUSTER\r\n\$4\r\nINFO\r\n"
    "*2\r\n\$7\r\nCLUSTER\r\n\$5\r\nNODES\r\n"
    "*2\r\n\$7\r\nCLUSTER\r\n\$5\r\nSLOTS\r\n"
    "*2\r\n\$7\r\nCLUSTER\r\n\$4\r\nMYID\r\n"
    "*3\r\n\$7\r\nCLUSTER\r\n\$7\r\nKEYSLOT\r\n\$7\r\ntestkey\r\n"
)

echo "📝 说明:"
echo "1. 这个脚本将验证已修复的功能"
echo "2. 需要启动相应的Redis-rs2服务器来进行测试"
echo "3. 哈希操作和DECRBY可以在优化服务器上测试"
echo "4. CLUSTER命令需要集群服务器"
echo ""

echo "🚀 启动服务器建议:"
echo "对于哈希和DECRBY测试:"
echo "  cargo run --bin simple_optimized_server 127.0.0.1:6385"
echo ""
echo "对于CLUSTER命令测试:"
echo "  cargo run --bin cluster_redis_server cluster.toml"
echo ""

echo "⚠️  注意: 请手动启动服务器后运行具体的测试命令"
echo ""

# 提供具体的测试命令示例
echo "📋 测试命令示例："
echo ""
echo "测试哈希操作:"
echo 'printf "*3\r\n\$4\r\nHSET\r\n\$6\r\nmyhash\r\n\$5\r\nfield1\r\n\$6\r\nvalue1\r\n" | nc localhost 6385'
echo 'printf "*3\r\n\$4\r\nHGET\r\n\$6\r\nmyhash\r\n\$5\r\nfield1\r\n" | nc localhost 6385'
echo ""
echo "测试DECRBY:"  
echo 'printf "*3\r\n\$3\r\nSET\r\n\$7\r\ncounter\r\n\$2\r\n10\r\n" | nc localhost 6385'
echo 'printf "*3\r\n\$6\r\nDECRBY\r\n\$7\r\ncounter\r\n\$1\r\n3\r\n" | nc localhost 6385'
echo ""
echo "测试CLUSTER命令:"
echo 'printf "*2\r\n\$7\r\nCLUSTER\r\n\$4\r\nINFO\r\n" | nc localhost 6379'
echo 'printf "*2\r\n\$7\r\nCLUSTER\r\n\$5\r\nSLOTS\r\n" | nc localhost 6379'
echo ""

echo "✅ 验证脚本准备完成"